package com.example.baseutils

import android.app.Activity
import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast

import com.example.baseutils.activity.BaseActivity
import com.example.baseutils.application.BaseApplication
import java.util.concurrent.CopyOnWriteArrayList

/**
 * Author by lp,on 2020/9/1/001,10:04.
 */

fun String.showMyToast(activity: Activity?, xlocation: Int = 0, ylocation: Int = 60) {
    val inflater = activity?.layoutInflater
    val view =
        inflater?.inflate(R.layout.bg_toast, null)
    val tv_msg = view?.findViewById(R.id.tv_msg) as TextView
    tv_msg.text = this
    val toast = Toast(BaseApplication.instance)
    toastList.add(toast)
    toast.setGravity(Gravity.CENTER, xlocation, ylocation)

    toast.duration = Toast.LENGTH_SHORT

    toast.setView(view)

    toast.show()
}

private val toastList= CopyOnWriteArrayList<Toast>()
fun removeAllToast(){
    if(toastList.isEmpty())
        return
    toastList.forEach {
        it.cancel()
    }
    toastList.clear()
}
